import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    indexFlag: false,
    title: "连云港特种设备安全集控平台", 
    areaList: JSON.parse(localStorage.getItem("areaList")) || [],           // 公共区域列表
    devList: JSON.parse(localStorage.getItem("devList")) || [],             // 公共设备类型列表
    hiddenLevel: JSON.parse(localStorage.getItem("hiddenLevel")) || [],     // 公共设备类型列表
    tasksLevel: JSON.parse(localStorage.getItem("tasksLevel")) || [],       // 公共设备类型列表
    listTitle: JSON.parse(localStorage.getItem("listTitle")) || '',         // 列表页title
    listData: JSON.parse(localStorage.getItem("listData")) || {},           // 列表页列表数据 *
    listPageType: JSON.parse(localStorage.getItem("listPageType")) || [],   // 列表页类型下拉
    listTotal: JSON.parse(localStorage.getItem("listTotal")) || 0,          // 列表页总数
    listAreaIndex: JSON.parse(localStorage.getItem("listAreaIndex")) || 0,  // 区域索引 （首页和列表页通用） 
    listTypeIndex: JSON.parse(localStorage.getItem("listTypeIndex")) || -1, // 设备类型索引 （首页和列表页通用）
    listCategory: JSON.parse(localStorage.getItem("listCategory")) || "",   // 列表分类名
    mapType: JSON.parse(localStorage.getItem("mapType")) || "lyg",          // 地图key
    rescueType: JSON.parse(localStorage.getItem("rescueType")) || [],       // 应急指挥用事故类型
  },

  getters: {
    getTitle: (state) => {
      return state.title || "";
    },
    getListData: (state) => {
      return state.listData || {};
    },
    getListPageType: (state) => {
      return state.listPageType || [];
    },
    getListTitle: (state) => {
      return state.listTitle || "";
    },
    getListToTal: (state) => {
      return state.listTotal;
    },
    getListAreaIndex: (state) => {
      return state.listAreaIndex;
    },
    getListTypeIndex: (state) => {
      return state.listTypeIndex;
    },
    getListCategory: (state) => {
      return state.listCategory;
    },
    getMapType: (state) => {
      return state.mapType;
    },
    getAreaList: (state) => {
      return state.areaList;
    },
    getDevList: (state) => {
      return state.devList;
    },
    getHiddenLevel: (state) => {
      return state.hiddenLevel;
    },
    getTasksLevel: (state) => {
      return state.tasksLevel;
    },
    getRescueType: (state) => {
      return state.rescueType;
    },
  },

  mutations: {
    setIndexFlag(state, flag) {
      state.indexFlag = flag;
    },
    changeTitle(state, str) {
      state.title = str;
    },
    setAreaList(state, arr) {
      localStorage.setItem("areaList", JSON.stringify(arr));
      state.areaList = arr;
    },
    setDevList(state, arr) {
      localStorage.setItem("devList", JSON.stringify(arr));
      state.devList = arr;
    },
    setHiddenLevel(state, arr) {
      localStorage.setItem("hiddenLevel", JSON.stringify(arr));
      state.hiddenLevel = arr;
    },
    setTasksLevel(state, arr) {
      localStorage.setItem("tasksLevel", JSON.stringify(arr));
      state.tasksLevel = arr;
    },
    setListTitle(state, str) {
      localStorage.setItem("listTitle", JSON.stringify(str));
      state.listTitle = str;
    },
    setListData(state, arr) {
      localStorage.setItem("listData", JSON.stringify(arr));
      state.listData = arr;
    },
    setListPageType(state, arr) {
      localStorage.setItem("listPageType", JSON.stringify(arr));
      state.listPageType = arr;
    },
    setListTotal(state, num) {
      localStorage.setItem("listTotal", JSON.stringify(num));
      state.listTotal = num;
    },
    setListAreaIndex(state, index) {
      localStorage.setItem("listAreaIndex", JSON.stringify(index));
      state.listAreaIndex = index;
    },
    setListTypeIndex(state, index) {
      localStorage.setItem("listTypeIndex", JSON.stringify(index));
      state.listTypeIndex = index;
    },
    setListCategory(state, type) {
      localStorage.setItem("listCategory", JSON.stringify(type));
      state.listCategory = type;
    },
    setMapType(state, type) {
      localStorage.setItem("mapType", JSON.stringify(type));
      state.mapType = type;
    },
    setRescueType(state, type) {
      localStorage.setItem("rescueType", JSON.stringify(type));
      state.rescueType = type;
    },
    setIndexType(state, indexType) {
      console.log("set index type");
      state.listData.body.forEach((element) => {
        element["indexType"] = indexType;
      });
    },
    clearIndex(state) {
      // localStorage.setItem("listAreaIndex", JSON.stringify(0));
      // localStorage.setItem("listTypeIndex", JSON.stringify(-1));
      localStorage.setItem("listCountPath", "");
      localStorage.setItem("listData", JSON.stringify({}));
      localStorage.setItem("listPageType", JSON.stringify([]));
      // state.listAreaIndex = 0;
      // state.listTypeIndex = -1;
    },
  },

  actions: {
    changeTitle(context, str) {
      context.commit("changeTitle", str);
    },
  },
});
